﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace VideoSpider.Module.SQLManager
{
    public interface SQLObject
    {
        public string InsertString();
        public string FindString();
        public string DeleteString();
        public string UpdateString();
    }

    public class SQLManager
    {
        public string userName;
        public string password;
        public string host;
        public string dataSourceName;
        public string port;

        public virtual bool Connection()
        {
            return false;
        }

        public virtual void Close()
        {

        }

        public virtual StringBuilder ConnectionString()
        {
            System.Diagnostics.Debug.Assert(this.dataSourceName != null && this.dataSourceName != string.Empty, "数据库名不能为空！");
            System.Diagnostics.Debug.Assert(this.password != null, "密码不能为空！");
            System.Diagnostics.Debug.Assert(this.host != null && this.host != string.Empty, "host不能为空！");
            System.Diagnostics.Debug.Assert(this.userName != null && this.userName != string.Empty, "用户名不能为空！");
            System.Diagnostics.Debug.Assert(this.port != null && this.port != string.Empty, "端口不能为空！");
            return null;
        }

        public virtual void Insert(SQLObject sqlObject)
        {
    
        }
    }

    public class MySqlManager : SQLManager
    {
        MySqlConnection mySqlConnection = null;

        public override void Insert(SQLObject sqlObject)
        {
            base.Insert(sqlObject);
            this.ExecuteCommand(sqlObject.InsertString());
        }

        private void ExecuteCommand(string command)
        {
            MySqlCommand sqlCommand = new MySqlCommand(command, this.mySqlConnection);
            sqlCommand.ExecuteNonQuery();
        }

        public override StringBuilder ConnectionString()
        {
            base.ConnectionString();
            StringBuilder sb = new StringBuilder();
            sb.Append("Database =");
            sb.Append(this.dataSourceName + ";");
            sb.Append("Data Source =");
            sb.Append(this.host + ";");
            sb.Append("User Id =");
            sb.Append(this.userName + ";");
            sb.Append("Password =");
            sb.Append(this.password + ";");
            sb.Append("port =");
            sb.Append(this.port + ";");
            return sb;
        }

        public override bool Connection()
        {
            if (this.mySqlConnection != null)
            {
                if (this.mySqlConnection.State == System.Data.ConnectionState.Open)
                {
                    return true;
                }
                else
                {
                    this.mySqlConnection.Open();
                }
            }
            else
            {
                this.mySqlConnection = new MySqlConnection(this.ConnectionString().ToString());
                this.mySqlConnection.Open();
            }
            return this.mySqlConnection.State == System.Data.ConnectionState.Open;
        }
    }
}
